﻿<%@ Page Title="Frontdesk - Form Field Editor" Language="C#" MasterPageFile="~/Views/Shared/Frontdesk.Master"
    Inherits="System.Web.Mvc.ViewPage<IEnumerable<Sendz.Domain.FormField>>" %>

<asp:Content ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Edit Form Fields</h2>
    <% if (Model.Count() == 1 && Convert.ToBoolean(ViewData.Eval("IsNewForm")))
       { %>
    <p class="clear justify critical">
        If you want to use an AutoResponder to reply to people who fill out your form, don't
        forget to include a required email field!
    </p>
    <% } %>
    <% using (Ajax.BeginForm(new AjaxOptions { OnBegin = "onBegin", UpdateTargetId = "wrapper", InsertionMode = InsertionMode.Replace }))
       { %>
    <div id="form-field-list">
        <% foreach (var field in Model)
           { %>
        <% Html.RenderPartial("form_field", field, ViewData); %>
        <% } %>
    </div>
    <div class="form-actions">
        <%: Html.ActionLink("Add a Field", "AddFormField", new { formId = ViewContext.FormContext.FormId, formKey = ViewData["FormKey"] }, new { id = "add-form-field", title = "Add a new form field to this form" })%>
    </div>
    <div class="buttons">
        <%: Html.ValidationSummary(true, "Your form fields need a little more work! Scroll up for more info...") %>
        <input type="submit" value="Save Form Fields" title="Save this form's fields and return to the dashboard"
            class="button" />
        <input type="button" value="Edit Form" class="button" title="Don't save, but go back to edit your form's general information instead"
            onclick="window.location = '<%: Url.Action("Edit", new { id = ViewData["FormKey"] } ) %>';" />
        <input type="button" value="Cancel" class="button" title="Don't save, but go back to the dashboard instead"
            onclick="window.location = '/Form/List';" />
    </div>
    <%} %>
</asp:Content>
<asp:Content ContentPlaceHolderID="Sidebar" runat="server">
    <% Html.RenderPartial("videohelp"); %>
</asp:Content>
<asp:Content ContentPlaceHolderID="footer" runat="server">
    <script type="text/javascript">
        $(document).ready(function () {
            // Allow sorting
            $("#form-field-list").sortable({
                handle: '.handle'
            });
            // Hide the delete option if there's only one form field
            if ($("#form-field-list > div.form-field").size() == 1) {
                $("a.delete-form-field").hide();
            }
            // Dynamically add a new form field when clicked
            $("#add-form-field").click(function () {
                $.ajax({
                    url: this.href,
                    cache: false,
                    success: function (html) {
                        $("#form-field-list").append(html);
                        // Show the delete option when there are multiple fields
                        if ($("#form-field-list > div.form-field").size() > 1) {
                            $("a.delete-form-field").show();
                        }
                        $(window).humanMsg("A new form field was added");
                    }
                });
                return false;
            });
            // Remove a form field
            $("a.delete-form-field").live("click", function () {
                if (confirm("Are you sure you wish to remove this form field? Data will no longer be visible in your reports!")) {
                    $(this).parents("div.form-field:first").remove();
                    // Hide the delete option on the last form field
                    if ($("#form-field-list > div.form-field").size() == 1) {
                        $("a.delete-form-field").hide();
                    }
                    $(window).humanMsg("Your form field was deleted");
                }
                return false;
            });
        });
    </script>
</asp:Content>
